package com.mls.guardian.appst.chain.excutor;

import com.mls.guardian.appst.chain.TaskExecutor;
import com.mls.guardian.appst.chain.TaskInvoker;
import com.mls.guardian.appst.chain.TaskResult;
import com.mls.guardian.appst.shell.ShellScriptExecutorFuture;
import com.mls.guardian.dataobj.ModuleMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by pengrenfang on 15/10/29.
 */
public class CompileWebExcutor implements TaskExecutor {
    private static final Logger log = LoggerFactory.getLogger(CompileWebExcutor.class);

    @Override
    public TaskResult exec(TaskInvoker invoker) {


        TaskResult result = null;
        try {
            log.info("----开始gradle war----");
            if (invoker.getModuleList() != null && invoker.getModuleList().size() > 0) {
                //指定模块
                for (ModuleMessage item : invoker.getModuleList()) {
                    log.info("----gradle war[{}]----", item.getModuleName());
                    String buildFile = invoker.getProject().getProjectPath() + File.separator + item.getModuleName() + File.separator + "build.gradle";

                    List<String> list = new ArrayList<String>();
                    list.add("gradle -b " + buildFile + " clean");
                    list.add("gradle -b " + buildFile + " war");
                    list.add("exit");

                    result = new ShellScriptExecutorFuture(list).execute();
                    if (result.isSuc() == false) {
                        if (result.getOut().indexOf("SUCCESSFUL") > -1) {//gradle比较特殊
                            result.setSuc(true);
                        }
                    }

                    //一旦报错,停止循环
                    if (result.isSuc() == false) {
                        break;
                    }
                }

            } else {
                //所有模块
                for (String item : invoker.getProject().getWebModuleList()) {
                    log.info("----gradle war[{}]----", item);
                    String buildFile = invoker.getProject().getProjectPath() + File.separator + item + File.separator + "build.gradle";

                    List<String> list = new ArrayList<String>();
                    list.add("gradle -b " + buildFile + " clean");
                    list.add("gradle -b " + buildFile + " war");
                    list.add("exit");

                    result = new ShellScriptExecutorFuture(list).execute();
                    if (result.isSuc() == false) {
                        if (result.getOut().indexOf("SUCCESSFUL") > -1) {//gradle比较特殊
                            result.setSuc(true);
                        }
                    }

                    //一旦报错,停止循环
                    if (result.isSuc() == false) {
                        break;
                    }

                }
            }

            log.info("----结束gradle war[{}]----", result.isSuc());

            if (result.isSuc() == true) {
                return invoker.invoke();
            }
            System.out.println(result.isSuc() + ":" + result.getError());
        } catch (Exception e) {
            result = new TaskResult();
            result.setSuc(false);
            result.setError(e.getMessage());

            e.printStackTrace();
        }

        return result;
    }
}
